<?php
require_once("Tool.php");

Class PrayList{

  public $praylist;
  public $pageSize;
  
  function __construct($size) {
  	$this->pageSize = $size;
		}	
		
  function getPray($page, $operator, $status)
  {
  	require_once("Dbcon.php");
	$dbc = new Dbcon();
  	$first = ($page - 1) * $this->pageSize;
	$last = $this->pageSize;
	
	$sql = "select * from pray where status " . $operator . " '" . $status . "' order by updated_date desc LIMIT " . $first . ", ". $last;
	$result = $dbc->getSelect($sql);
	$dbc->clear();	
	return $result;
  }
  
  function getPage($operator, $status)
  {
  	require_once("Dbcon.php");
	$dbc = new Dbcon();
	
	$sql = "select count(*) from pray where status " . $operator . " '" . $status . "' order by updated_date desc";
	$result = $dbc->getSelect($sql);
	$dbc->clear();	
	
	return ceil(reset(reset($result))/$this->pageSize);
  }
  
  function setPray($subject, $message, $prayer, $status)
  {
  	require_once("Dbcon.php");
  	$dbc = new Dbcon();
	
	$sql = "insert into pray(subject, message, prayer, status, created_date, updated_date) values('" . $this->encodeStr($subject) . "', '" . $this->encodeStr($message) . "', '" . $this->encodeStr($prayer) . "', '" . $status . "', DATE_ADD(NOW(), INTERVAL 15 HOUR), DATE_ADD(NOW(), INTERVAL 15 HOUR))";
	$result = $dbc->setInsert($sql);
	$dbc->clear();	
	
	return $result;
  }
  
  function updatePray($pid, $subject, $message, $prayer, $status)
  {
  	require_once("Dbcon.php");
  	$dbc = new Dbcon();
	
	$sql = "update pray set subject = '". $this->encodeStr($subject) . "', message = '" . $this->encodeStr($message) . "', prayer = '" . $this->encodeStr($prayer) . "', status = '" . $status . "' where pid = " . $pid;
	$result = $dbc->setUpdate($sql);
	$dbc->clear();	
	
	return $result;
  }
  
  function approvePray($pid, $status)
  {
  	require_once("Dbcon.php");
  	$dbc = new Dbcon();
	
	$sql = "update pray set status = '" . $status . "' where pid = " . $pid;
	$result = $dbc->setUpdate($sql);
	$dbc->clear();	
	
	return $result;
  }
  
  function delPray($pid)
  {
  	require_once("Dbcon.php");
  	$dbc = new Dbcon();
	
	$sql = "update pray set status = 'closed' where pid = " . $pid;
	$result = $dbc->setUpdate($sql);
	$dbc->clear();	
	
	return $result;
  }
  
  public function encodeStr($str)
  {
  	return Tool::encode($str);
  }	
	
}
?>